package controllers;

import play.mvc.*;
import play.data.*;
import static play.data.Form.*;
import views.html.pageErreurConnexion;
import views.html.ConsoleAdmin.*;

import models.*;

/**
 * Manage a database of qcmNiveauQuestions
 */
public abstract class AdminQcmNiveauQuestion extends Controller {

	/**
	 * This result directly redirect to application home.
	 */
	public final static Result GO_HOME = redirect(routes.AdminQcmNiveauQuestion
			.listQcmNiveauQuestion(0, "id_qcm_niveau_question", "asc", ""));

	/**
	 * Handle default path requests, redirect to qcmNiveauQuestions list
	 */
	public static Result indexQcmNiveauQuestion() {
		if (Application.estAdmin()) {
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Display the paginated list of qcmNiveauQuestions.
	 * 
	 * @param page
	 *            Current page number (starts from 0)
	 * @param sortBy
	 *            Column to be sorted
	 * @param order
	 *            Sort order (either asc or desc)
	 * @param filter
	 *            Filter applied on qcmNiveauQuestion names
	 */
	public static Result listQcmNiveauQuestion(final int page,
			final String sortBy, final String order, final String filter) {
		if (Application.estAdmin()) {
			return ok(listQcmNiveauQuestion.render(
					QcmNiveauquestion.page(page, 10, sortBy, order, filter),
					sortBy, order, filter));
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Display the 'edit form' of a existing QcmNiveauquestion.
	 * 
	 * @param id
	 *            Id of the qcmNiveauQuestion to edit
	 */
	public static Result editQcmNiveauQuestion(final Long id) {
		if (Application.estAdmin()) {
			final Form<QcmNiveauquestion> qcmNiveauQuestionForm = form(
					QcmNiveauquestion.class).fill(
					QcmNiveauquestion.findById(id));
			return ok(editQcmNiveauQuestion.render(id, qcmNiveauQuestionForm));
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Handle the 'edit form' submission
	 * 
	 * @param id
	 *            Id of the qcmNiveauQuestion to edit
	 */
	public static Result updateQcmNiveauQuestion(final Long id) {
		if (Application.estAdmin()) {
			final Form<QcmNiveauquestion> qcmNiveauQuestionForm = form(
					QcmNiveauquestion.class).bindFromRequest();
			if (qcmNiveauQuestionForm.hasErrors()) {
				return badRequest(editQcmNiveauQuestion.render(id,
						qcmNiveauQuestionForm));
			}

			qcmNiveauQuestionForm.get().update(id);
			flash("success", "QcmNiveauquestion "
					+ qcmNiveauQuestionForm.get().getIdQcmNiveauQuestion()
					+ " has been updated");
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Display the 'new qcmNiveauQuestion form'.
	 */
	public static Result createQcmNiveauQuestion() {
		if (Application.estAdmin()) {
			final Form<QcmNiveauquestion> qcmNiveauQuestionForm = form(QcmNiveauquestion.class);
			return ok(createQcmNiveauQuestion.render(qcmNiveauQuestionForm));
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Handle the 'new qcmNiveauQuestion form' submission
	 */
	public static Result saveQcmNiveauQuestion() {
		if (Application.estAdmin()) {
			final Form<QcmNiveauquestion> qcmNiveauQuestionForm = form(
					QcmNiveauquestion.class).bindFromRequest();
			if (qcmNiveauQuestionForm.hasErrors()) {
				return badRequest(createQcmNiveauQuestion
						.render(qcmNiveauQuestionForm));
			}
			qcmNiveauQuestionForm.get().save();
			flash("success", "QcmNiveauquestion "
					+ qcmNiveauQuestionForm.get().getIdQcmNiveauQuestion()
					+ " has been created");
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Handle qcmNiveauQuestion deletion
	 */
	public static Result deleteQcmNiveauQuestion(final Long id) {
		if (Application.estAdmin()) {
			QcmNiveauquestion.delete(id);
			flash("success", "QcmNiveauquestion has been deleted");
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

}
